#include "main.h"

extern gchar *log_file;

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  debug_print_root
 *  Description:  print root struture
 * =====================================================================================
 */
void debug_print_root (GSList *root)
{
  // debug
  int i, j;
  int t = g_slist_length (root);

  g_debug ("\n\nmemory dump:----->");

  for (i = 0; i < t; i++) {
    TreeAttr *attr = g_slist_nth_data (root, i);
    g_debug ("\nexpr %d : type=%d, len=%d, expr=\"%s\"", i, \
        attr->type, \
        attr->expr->len, \
        attr->expr->str);
    if (attr->head) {
      for (j = 0; j < g_slist_length (attr->head); j++) {
        g_debug ("[%d] : (%s)", j, ((GString *)g_slist_nth_data (attr->head, j))->str);
      }
    }

  }

  g_debug ("<------dump end\n\n");
}


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  debug_print_map
 *  Description:  print bitmap of root
 * =====================================================================================
 */
void debug_print_map (GSList *root)
{
  GSList *list = root;
  gchar buf[40];

  g_debug ("\n\nmap dump: ------>");

  while (list) {
    GSList *next = list->next;
    TreeAttr *tree = list->data;
    
    g_debug ("[%d]: point = %d map = %s len = %d", \
        tree->index, tree->point, itoa(tree->map, buf, 2), \
        g_slist_length (tree->head));

    list = next;
  }

  g_debug ("\n<------------dump end\n\n");
}


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  debug_redirect
 *  Description:  
 * =====================================================================================
 */
void log_redirect (const gchar *log_domain, \
                     GLogLevelFlags log_level, \
                     const gchar *message, \
                     gpointer user_data)
{
  gint written;
  GError *error = NULL;

  GIOChannel *log = g_io_channel_new_file (log_file, "a+", &error);

  GString *str = g_string_new (message);
  g_string_append_c (str, '\n');

  if (!log) {
    g_print ("Can't open log file : %s\n", error->message);
    g_error_free (error);
    return;
  }

  GIOStatus status = g_io_channel_write_chars (log, str->str, str->len, &written, &error);
  if (status != G_IO_STATUS_NORMAL) {
    g_print ("Write log failed : %s\n", error->message);
    g_error_free (error);
  }

  g_string_free (str, TRUE);
  g_io_channel_shutdown (log, TRUE, &error);

}


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  log_dummy_redirect
 *  Description:  
 * =====================================================================================
 */
void log_dummy_redirect (const gchar *log_domain, \
                        GLogLevelFlags log_level, \
                        const gchar *message, \
                        gpointer user_data)
{
  return;
}
